package com.pig4cloud.pig.common.swagger.plugins;

import com.bblocks.common.Constant;
import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;

import java.lang.reflect.Field;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>Project: bblocks-pro - ParameterBuilderComm</p>
 * <p>描述：ParameterBuilderComm 插件统一定义</p>
 *
 * @Author Zhao [125043150@qq.com]
 * @Date 2024/3/2 21:03
 * @Version 1.0
 * @since 8
 */
public class ParameterBuilderComm {

    /**
     * 动态生成的Class的包路径 自由定义（必须存在的包路径 否则可能错）
     */
    protected final static String BASE_PACKAGE = "com.pig4cloud.pig.common.swagger.plugins";//com.bblocks.common.swagger.params

    /**
     * 默认类名
     */
    protected final static String DEFAULT_CLASS_NAME =   "RequestBody";
    /**
     * 序号 防止重名
     */
    protected static AtomicInteger paramsIndex = new AtomicInteger(0);

    /**
     * 尽量提取（）
     * @param field
     * @return
     * @throws NotFoundException
     */
    protected CtClass typeTry(Field field) throws NotFoundException {
        ClassPool pool = ClassPool.getDefault();
        CtClass type = pool.getOrNull(field.getType().getName());
        if(type == null){
            ClassClassPath classPath = new ClassClassPath(field.getType());
            pool.insertClassPath(classPath);
            type = pool.get(field.getType().getName());
        }
        return type;
    }

}
